import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import router from '@/router/index.js';
import store from '@/store';
NProgress.configure({ showSpinner: false });
const whiteList = ['/login', '/auth-redirect'];
// 路由进度和路由拦截
router.beforeEach(async(to, from, next) => {
  NProgress.start();
  // console.log(1111, sessionStorage.getItem('userToken'));
  if (sessionStorage.getItem('userToken')) {
    if (to.path === '/login' || to.path === '/layout'){
      next();
    } else {
      console.log('newRoutes', to.path);
      // 浏览器刷新时执行

      store.dispatch('user/getUserInfo', sessionStorage.getItem('userToken')).then((res) => {
      }).catch((err) => {
        console.log(err);
      });
      next();
    }
    NProgress.done();
  } else {
    console.log('no token');
    /* has no token*/
    // 当前路径在白名单里面出现
    if (whiteList.indexOf(to.path) !== -1) {
      next();
      NProgress.done();
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`);
      NProgress.done();
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});
